package general;

class NumberEncoding
{

    // this class is assuredly suboptimal. However, it has relatively slim 
    // processor overhead, which is probably more important than bandwidth
    // these days.

    // Ints are encoded as a hex string using haxe built-ins. Negative
    // numbers have a - character appended to them. 

    // Floats are even simpler: they're handled purely as strings.
    
    public static function encodeInt(num : Int) : String
    {
        var result : String;
        if (num<0)
            result = "-";
        else
            result = "";
        result += StringTools.hex(Std.int(Math.abs(num)));
        return result;
    }

    public static function decodeInt(input : String) : Int
    {
       if (input.charAt(0)=="-")
       {
          return -1*Std.parseInt("0x"+input.substr(1));
       }
       else
       {
          return Std.parseInt("0x"+input);            
       }
    }

    public static inline function encodeFloat(num : Float) : String
    {
        return Std.string(num);
    }

    public static inline function decodeFloat(input : String) : Float
    {
        return Std.parseFloat(input);
    }

}